<?php
// $current_cat_id: the current category id number
// $count: just a counter, call it as 0 in your function call and forget about it
/* GET THE DROP DOWN LIST OF CATEGORIES */
$conn = mysql_connect( mysql_host, mysql_user, mysql_password )  or die(mysql_error());
mysql_select_db(mysql_database, $conn) or die(mysql_error());
mysql_query("SET character_set_results=utf8", $conn);   // show data VNs

function get_cat_selectlist($current_cat_id, $count) {
	static $option_results;

	// if there is no current category id set, start off at the top level (zero)

	if (!isset($current_cat_id)) {

		$current_cat_id =0;
	}

	// increment the counter by 1

	$count = $count+1;

	// query the database for the sub-categories of whatever the parent category is

	$query =  'SELECT id, name from product_category where parents='.$current_cat_id;
	$query .=  'order by `order` asc ';

	echo $query;
	#exit;

	$num_options = mysql_num_rows($query);

	// our category is apparently valid, so go ahead €¦
	if ($num_options > 0) {
		while (list($id, $name) = mysql_fetch_row($query)) {
			// if its not a top-level category, indent it to
			//show that its a child category
			if ($current_cat_id!=0) {
				$indent_flag =  '--';
				for ($x=2; $x<=$count; $x++) {
					$indent_flag .=  ' >';
				}
			}

			$name = $indent_flag.$name;

			$option_results[$id] = $name;

			// now call the function again, to recurse through the child categories

			get_cat_selectlist($id, $count );
		}
	}
	return $option_results;
}

echo "<select name='cat_id1'>";

echo "<option value=''>-- Select -- </option>";

$get_options = get_cat_selectlist(0, 0);

if (count($get_options) > 0){
	$categories = $_POST['cat_id'];
	foreach ($get_options as $key => $value) {
		$options .="<option value=\"$key\"";
		// show the selected items as selected in the listbox
		if ($_POST['cat_id'] == "$key") {

			$options .=" selected=\"selected\"";
		}
		$options .=">$value</option>\n";
	}
}
echo $options;

echo "</select>";
?>